/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author kleks
 */
package FilterCommon;


import java.awt.*;
import java.awt.Graphics;

public class FilterView
{
    static final int H=90;//filter size
    static final int W=60;

    static final int h=10;//in out size
    static final int w=15;

    static final int b=1;//border size

    int inCount;
    int outCount;

    int x;
    int y;

    int dragFromX;
    int dragFromY;

    private boolean selected;
    private int     selectedIn;
    private int     selectedOut;
    
    public FilterView (int inCount, int outCount)
    {   this.inCount = inCount;

        this.outCount = outCount;
    }

    public void paintFilter(Graphics g)
    {
        int bf = selected?b*2:b;
        int bio;

        
        g.setColor(Color.BLACK);
        g.fillRect(x, y, W, H);
        if(isSelected() == true)
        {
            g.setColor(Color.YELLOW);
        }
        else
        {
            g.setColor(Color.WHITE);
        }
        g.fillRect(x+bf, y+bf, W-2*bf, H-2*bf);

        
        for(int i=0; i<inCount; i++)
        {   bio = selectedIn==i?b*2:b;
            g.setColor(Color.BLACK);
            g.fillRect(getInX(i), getInY(i), w, h);
            g.setColor(Color.GREEN);
            g.fillRect(getInX(i)+bf, getInY(i)+bio, w-bf-bio, h-2*bio);

        }

        for(int i=0; i<outCount; i++)
        {   bio = selectedOut==i?b*2:b;
            g.setColor(Color.BLACK);
            g.fillRect(getOutX(i), getOutY(i), w, h);
            g.setColor(Color.GREEN);
            g.fillRect(getOutX(i)+bio, getOutY(i)+bio, w-bf-bio, h-2*bio);
        }
    
    }

    boolean onFilter(int xx, int yy)
    {
        return (xx >= x && xx < (x + W)
                && yy >= y && yy < (y + H));
    }

    private boolean setFilterIn(int xx, int yy)
    {   for(int i=0; i<inCount; i++)
        {
            if(xx>=getInX(i) && xx < (getInX(i)+w) && yy>=getInY(i) && yy<(getInY(i)+h))
            {   selectedIn = i;
//                System.out.print("setIn "+i+"\n");
                return true;
            }
        }
        return false;
    }

    private boolean setFilterOut(int xx, int yy)
    {   for(int i=0; i<outCount; i++)
        {
            if(xx>=getOutX(i) && xx < (getOutX(i)+w) && yy>=getOutY(i) && yy<(getOutY(i)+h))
            {   selectedOut = i;
//                System.out.print("setOut "+i+"\n");
                return true;
            }
        }
        return false;
    }


    int getInX(int i)
    {   return x;
    }

    int getInY(int i)
    {   return (int)(y+b+(i+0.5)*(H-2*b)/inCount)-h/2;
    }

    int getOutX(int i)
    {   return x+W-w;
    }

    int getOutY(int i)
    {   return (int)(y+b+(i+0.5)*(H-2*b)/outCount)-h/2;
    }

    int getInConnectionX(int i)
    {   return x;
    }

    int getInConnectionY(int i)
    {   return (int)(y+b+(i+0.5)*(H-2*b)/inCount)-h/2;
    }

    int getOutConnectionX(int i)
    {   return x+W-w;
    }

    int getOutConnectionY(int i)
    {   return (int)(y+b+(i+0.5)*(H-2*b)/outCount)-h/2;
    }

    /**
     * @return the selected
     */
    public boolean isSelected() {
        return selected;
    }

    /**
     * @param selected the selected to set
     */
    public void setSelected(boolean selected)
    {
        this.selected = selected;
//        if(selected)
//        {   setFilterIn(x, y);
//            setFilterOut(x, y);
//
//        }
//        else
        {
            this.selectedIn=-1;
            this.selectedOut=-1;
        }
    }
    public void setSelected(boolean selected, int x, int y)
    {
//        System.out.print("setSelected "+x+","+y+"\n");
        this.selected = selected;
        if(selected)
        {   setFilterIn(x, y);
            setFilterOut(x, y);

        }
        else
        {
            this.selectedIn=-1;
            this.selectedOut=-1;
        }
    }

}
